//package wu-framework-lazy-orm-solon-starter.aop;
//
//import lombok.NonNull;
//
//import org.wu.framework.core.aop.Advice;
//import org.wu.framework.core.aop.Pointcut;
//import org.wu.framework.core.aop.intercept.MethodInterceptor;
//import org.wu.framework.core.aop.support.AbstractPointcutAdvisor;
//import org.wu.framework.lazy.orm.database.lambda.dynamic.toolkit.DynamicLazyDSContextHolder;
//import org.wu.framework.lazy.orm.core.persistence.conf.ClassLazyDynamicEndpoint;
//import org.wu.framework.lazy.orm.core.stereotype.LazyDS;
//
//import java.lang.reflect.Method;
//
///**
// * 切换数据源
// *
// * @author : Jia wei Wu
// * @version : 1.0
// * @date : 2021/7/4 6:05 下午
// */
//public class LazyDynamicPointcutAdvisor extends AbstractPointcutAdvisor implements BeanFactoryAware {
//    private final Advice advice;
//
//    private final Pointcut pointcut;
//
//    public LazyDynamicPointcutAdvisor(@NonNull LazyDBInterceptor lazyDBInterceptor) {
//        this.advice = lazyDBInterceptor;
//        this.pointcut = buildPointcut();
//    }
//
//    private Pointcut buildPointcut() {
//        Pointcut cpc = new AnnotationMatchingPointcut(LazyDS.class, true);
//        Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(LazyDS.class);
//        Pointcut classAnnotation = AnnotationMatchingPointcut.forClassAnnotation(LazyDS.class);
//        return new ComposablePointcut(cpc).union(mpc).union(classAnnotation);
//    }
//
//    /**
//     * Get the Pointcut that drives this advisor.
//     */
//    @Override
//    public Pointcut getPointcut() {
//        return pointcut;
//    }
//
//
//    @Override
//    public Advice getAdvice() {
//        return advice;
//    }
//
//
//    @Override
//    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
//
//    }
//
//    /**
//     * description 切面处理
//     *
//     * @author Jia wei Wu
//     * @date 2020/9/11 上午9:28
//     */
//    public static class LazyDBInterceptor implements MethodInterceptor {
//        @Override
//        public Object invoke(MethodInvocation invocation) throws Throwable {
//            // 切换数据源
//            LazyDS lazyDS = determineLazyDS(invocation);
//            final ClassLazyDynamicEndpoint lazyDynamicEndpoint = new ClassLazyDynamicEndpoint();
//            lazyDynamicEndpoint.setName(lazyDS.name());
//            DynamicLazyDSContextHolder.push(lazyDynamicEndpoint);
//            try {
//                return invocation.proceed();
//            } finally {
//                DynamicLazyDSContextHolder.clear();
//            }
//        }
//
//        /**
//         * @param
//         * @return describe 确定 Lazy DS
//         * @author Jia wei Wu
//         * @date 2021/7/4 5:04 下午
//         **/
//        public LazyDS determineLazyDS(MethodInvocation invocation) {
//            Method method = invocation.getMethod();
//            Class<?> declaringClass = method.getDeclaringClass();
//            LazyDS lazyDS = method.isAnnotationPresent(LazyDS.class) ? method.getAnnotation(LazyDS.class)
//                    : AnnotationUtils.findAnnotation(declaringClass, LazyDS.class);
//            return lazyDS;
//        }
//
//    }
//
//}
